package com.whut.manage.utils;

import com.aliyun.iot20180120.models.GetDeviceStatusRequest;
import com.aliyun.iot20180120.models.GetDeviceStatusResponse;
import com.aliyun.iot20180120.models.GetDeviceStatusResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WZH
 * @create 2021-06-10 20:49
 * @desc 工具类
 **/
public class IotDevicesUtil {
    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    private static com.aliyun.iot20180120.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI5t6HvJEWHhmxibc8aaT6")
                // 您的AccessKey Secret
                .setAccessKeySecret("zlnj5d6Hp2Q32vhkDZhA8c7ok0KE6W");
        // 访问的域名
        config.endpoint = "iot.cn-shanghai.aliyuncs.com";
        return new com.aliyun.iot20180120.Client(config);
    }

    public static List<DeviceStatus> getDeviceStatusResponseBodyDataList() {
        List<DeviceStatus> responseBodyData = null;
        try {
            com.aliyun.iot20180120.Client client = IotDevicesUtil.createClient();
            String productKey = "a1otxckNJeW";
            String[] deviceNames = "4GGW0001,Gateway01,HL30003019,HL30003017,hexedge01,TestDev1".split(",");
            responseBodyData = new ArrayList<>();
            for (String deviceName : deviceNames) {
                GetDeviceStatusRequest getDeviceStatusRequest = new GetDeviceStatusRequest()
                        .setProductKey(productKey)
                        .setDeviceName(deviceName);
                GetDeviceStatusResponse resp = client.getDeviceStatus(getDeviceStatusRequest);
                if (resp != null && resp.getBody() != null && resp.getBody().getData() != null) {
                    GetDeviceStatusResponseBody.GetDeviceStatusResponseBodyData data = resp.getBody().getData();
                    responseBodyData.add(new DeviceStatus(deviceName, data.getStatus()));

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseBodyData;
    }


    public static void main(String[] args_) throws Exception {

        getDeviceStatusResponseBodyDataList().forEach(data -> {
            try {
                System.out.println((Common.toJSONString(data)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    static class DeviceStatus {
        private String id;
        private boolean status;

        public DeviceStatus() {
        }

        public DeviceStatus(String id, String status) {
            this.id = id;
            setStatus(status);
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public boolean isStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = "ONLINE".equals(status);
        }
    }

}
